package com.lb.service.impl;

import com.lb.dao.*;
import com.lb.entity.*;
import com.lb.service.TbCinemaService;
import com.lb.vo.SaLaVo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (TbCinema)表服务实现类
 *
 * @author libo
 * @since 2024-09-10 10:14:21
 */
@Service("tbCinemaService")
public class TbCinemaServiceImpl implements TbCinemaService {
    @Resource
    private TbCinemaDao tbCinemaDao;
    @Resource
    private TbMovieDao tbMovieDao;
    @Resource
    private TbSalaMovieDao tbSalaMovieDao;
    @Resource
    private TbSalaTimeDao salaTimeDao;

     @Resource
    private TbCinemaSalaDao tbCinemaSalaDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbCinema queryById(Integer id) {
        return this.tbCinemaDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param tbCinema    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<TbCinema> queryByPage(TbCinema tbCinema, PageRequest pageRequest) {
        long total = this.tbCinemaDao.count(tbCinema);
        return new PageImpl<>(this.tbCinemaDao.queryAllByLimit(tbCinema, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param tbCinema 实例对象
     * @return 实例对象
     */
    @Override
    public TbCinema insert(TbCinema tbCinema) {
        this.tbCinemaDao.insert(tbCinema);
        return tbCinema;
    }

    /**
     * 修改数据
     *
     * @param tbCinema 实例对象
     * @return 实例对象
     */
    @Override
    public TbCinema update(TbCinema tbCinema) {
        this.tbCinemaDao.update(tbCinema);
        return this.queryById(tbCinema.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tbCinemaDao.deleteById(id) > 0;
    }

    @Override
    public List<TbCinema> queryAll() {
        return tbCinemaDao.selectList();
    }

    @Override
    public List<TbCinema> queryDisCount(String movieId) {
        List<TbCinema> tbCinemas = queryAll();
        TbMovie tbMovie = tbMovieDao.queryById(Integer.valueOf(movieId));
      return tbCinemas.stream().map(item -> {
          item.setCinemaPrice(getCountPrice(item, tbMovie));
          return item;
      }).collect(Collectors.toList());
    }

    @Override
    public List<SaLaVo> querySaLaByMovieId(Integer movieId, Integer cinemaId) {
        TbCinema tbCinema = tbCinemaDao.queryById(cinemaId);
        TbMovie tbMovie = tbMovieDao.queryById(movieId);
       List<TbSalaMovie> salaMovies= tbSalaMovieDao.queryByMovieId(cinemaId,movieId);
       return salaMovies.stream().map(item ->
                 toSaLaVo(item, tbMovie, tbCinema)).
               flatMap(List::stream).
               collect(Collectors.toList());
    }
    public  List<SaLaVo> toSaLaVo(TbSalaMovie tbSalaMovie,TbMovie tbMovie,TbCinema tbCinema) {
        Integer id = tbSalaMovie.getId();
       List<TbSalaTime>tbSalaTimes=salaTimeDao.queryBySaLaMovieId(id);
        return tbSalaTimes.stream().map(item -> {
            SaLaVo saLaVo = new SaLaVo();
            saLaVo.setShowDate(tbSalaMovie.getShowDate());
            saLaVo.setSaLaId(item.getId());
            saLaVo.setLanguageType("国语/2D");
            saLaVo.setMoviePrice(BigDecimal.valueOf(item.getMoviePrice()));
            saLaVo.setShowTime(item.getShowTime());
            saLaVo.setTbMovie(tbMovie);
            saLaVo.setTbCinema(tbCinema);
            TbCinemaSala tbCinemaSala = tbCinemaSalaDao.queryById(item.getSalaId());
            saLaVo.setSalaName(tbCinemaSala.getSalaName());
            return saLaVo;
        }).collect(Collectors.toList());
    }
    static Double getCountPrice(TbCinema tbCinema, TbMovie tbMovie){
      Double cinemaPrice = tbCinema.getDiscount()/10;
      BigDecimal multiply = tbMovie.getMoviePrice().
              multiply(BigDecimal.valueOf(cinemaPrice)).
              setScale(2,BigDecimal.ROUND_DOWN);
      return Double.valueOf(multiply.toString());
  }

}
